﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbEmailDal
	{
		public DbEmailDal()
		{
		}
 		#region Insert
 		public static int Insert(DbEmail dbEmail)
		{	
			string sql = @"Insert Into DbEmail (
					EMail,
					EPwd,
					ESmtp,
					EPort,
					EShowEmail,
					EShowUser,
					EStatus,
					ERegisterMail,
					EOrderMail
				) values('{0}','{1}','{2}',{3},'{4}','{5}',{6},'{7}','{8}')";
			sql = string.Format(sql,
					SqlHelper.String(dbEmail.EMail),
					SqlHelper.String(dbEmail.EPwd),
					SqlHelper.String(dbEmail.ESmtp),
					dbEmail.EPort,
					SqlHelper.String(dbEmail.EShowEmail),
					SqlHelper.String(dbEmail.EShowUser),
					dbEmail.EStatus,
					dbEmail.ERegisterMail,
					dbEmail.EOrderMail
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbEmail dbEmail)
		{	
			string sql = @"Update DbEmail set 

					EMail= '{0}',
					EPwd= '{1}',
					ESmtp= '{2}',
					EPort= {3},
					EShowEmail= '{4}',
					EShowUser= '{5}',
					EStatus= {6},
					ERegisterMail= '{7}',
					EOrderMail= '{8}'
				where

					EId= {9}";
			sql = string.Format(sql,
					SqlHelper.String(dbEmail.EMail),
					SqlHelper.String(dbEmail.EPwd),
					SqlHelper.String(dbEmail.ESmtp),
					dbEmail.EPort,
					SqlHelper.String(dbEmail.EShowEmail),
					SqlHelper.String(dbEmail.EShowUser),
					dbEmail.EStatus,
					dbEmail.ERegisterMail,
					dbEmail.EOrderMail,
					dbEmail.EId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 eId)
		{
		
			string sql = @"Delete from DbEmail where 
					EId= {0}";
			sql = string.Format(sql,
					eId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbEmail a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbEmail Select(Int32 eId)
		{
			DbEmail dbEmail = null;
			string sql = @"select a.* from DbEmail a  where
					a.EId= {0}
";
			sql = string.Format(sql,
					eId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbEmail = new DbEmail(ds.Tables[0].Rows[0]);					
			return dbEmail;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbEmail a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbEmail a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbEmail a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
